package cn.eoeit.mmy.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import cn.eoeit.mmy.bean.Pagination;
import cn.eoeit.mmy.dao.IBaseDao;
import cn.eoeit.mmy.model.DesignInfo;
import cn.eoeit.mmy.model.GongXuModel;
import cn.eoeit.mmy.model.LinJianFactory;
import cn.eoeit.mmy.model.LinJianKeyInfo;
import cn.eoeit.mmy.model.LingJianGongXuModel;
import cn.eoeit.mmy.model.LingJianModel;
import cn.eoeit.mmy.model.LinjianSellInfo;
import cn.eoeit.mmy.model.UserInfo;
import cn.eoeit.mmy.service.IlingJianService;
import cn.eoeit.mmy.util.SysUtils;

@Service("lingJianService")
public class LingJianServiceImpl implements IlingJianService{
	@Autowired
	IBaseDao<LingJianModel> lingJianDao;
	
	@Autowired
	IBaseDao<GongXuModel> gongXuDao;
	
	@Autowired
	IBaseDao<LingJianGongXuModel> gongXuLjDao;
	
	@Autowired
	IBaseDao<LinJianFactory> factoryLjDao;
	
	@Autowired
	IBaseDao<DesignInfo> designInfoDao;
	
	@Autowired
	IBaseDao<LinjianSellInfo> sellInfoDao;
	
	@Autowired
	IBaseDao<LinJianKeyInfo> keyInfoDao;
	
	
	
	public IBaseDao<GongXuModel> getGongXuDao() {
		return gongXuDao;
	}

	public void setGongXuDao(IBaseDao<GongXuModel> gongXuDao) {
		this.gongXuDao = gongXuDao;
	}

	public IBaseDao<LingJianModel> getLingJianDao() {
		return lingJianDao;
	}

	public void setLingJianDao(IBaseDao<LingJianModel> lingJianDao) {
		this.lingJianDao = lingJianDao;
	}

	@Override
	public LingJianModel finishAddLingJian(LingJianModel lingJianModel) {
		lingJianModel.setLastUpdateTime(System.currentTimeMillis());
		lingJianDao.save(lingJianModel);
		 return lingJianModel;
	}

	@Override
	public LingJianModel findLingJian(Integer uid) {
		return lingJianDao.get(LingJianModel.class, uid);
	}

	@Override
	public Pagination<HashMap<String, Object>> selectAllLingJians(int page, int rows) {
		 String sql="select count(*) from lingjian ";
			long total=lingJianDao.countSql(sql);
			Pagination<HashMap<String,Object>> pagination=new Pagination<HashMap<String,Object>>(page,(int) total, rows);
			List<HashMap<String,Object>>result=new ArrayList<HashMap<String,Object>>();
			sql="select lingjian.id, lingjian.name, lingjian.descript, lingjian.bianHao, lingjian.xingHao, lingjian.riqi  from lingjian ";
			 List<Object>  datas = lingJianDao.findSql(sql, page, pagination.getPageSize());
			 for(Object obj:datas){
				 if(obj instanceof Object[]){
					 Object[]temp=(Object[])obj;
					 HashMap<String,Object> map= new HashMap<String,Object>();
					 map.put("id", temp[0]);
					 map.put("name", temp[1]);
					 map.put("descript", temp[2]);
					 map.put("bianHao", temp[3]);
					 map.put("xingHao", temp[4]);
					 map.put("riqi", temp[5]);
					 result.add(map);
				 }
			 }
			pagination.setDatas(result);
			return pagination;
	}

	@Override
	public LingJianModel finishDeleteLingJian(Integer uid) {
		
		//删除采购信息
		String hql="delete from LinJianFactory where linJianId=?";
		factoryLjDao.executeHql(hql, uid);
		//删除加工信息
		 hql="delete from LingJianGongXuModel where lingJianId=?";
		gongXuLjDao.executeHql(hql, uid);
		//删除设计信息
		 hql="delete from DesignInfo where lingJianID=?";
		 designInfoDao.executeHql(hql, uid);
		//删除销售信息
		 hql="delete from LinjianSellInfo where linJianID=?";
		 sellInfoDao.executeHql(hql, uid);
		 
		LingJianModel user=lingJianDao.get(LingJianModel.class, uid);
		if(user!=null){
			lingJianDao.delete(user);
		}
		return user;
	}

	@Override
	public LingJianModel finishUpdateLingJian(LingJianModel user) {
		LingJianModel lingJian=lingJianDao.get(LingJianModel.class, user.getId());
		if(lingJian!=null){
			lingJian.setLastUpdateTime(System.currentTimeMillis());
			lingJianDao.update(lingJian);
		}
		return lingJian;
	}

	@Override
	public GongXuModel finishAddGongXu(GongXuModel gongXuModel) {
		gongXuModel.setLastUpdateTime(System.currentTimeMillis());
		gongXuDao.save(gongXuModel);
		 return gongXuModel;
	}

	@Override
	public GongXuModel findGongXuModel(Integer uid) {
		return gongXuDao.get(GongXuModel.class, uid);
	}


	@Override
	public Pagination<HashMap<String, Object>> selectAllGongXuModels(int page, int rows) {
		   String sql="select count(*) from gong_xu ";
			long total=gongXuDao.countSql(sql);
			Pagination<HashMap<String,Object>> pagination=new Pagination<HashMap<String,Object>>(page,(int) total, rows);
			List<HashMap<String,Object>>result=new ArrayList<HashMap<String,Object>>();
			sql="select gong_xu.id ,gong_xu.name ,device_info.name as deviceName ,gong_xu.jiaJu,gong_xu.descript,gong_xu.lastUpdateTime from gong_xu ,device_info where gong_xu.sheBeiId= device_info.id ";
			 List<Object>  datas = gongXuDao.findSql(sql, page, pagination.getPageSize());
			 for(Object obj:datas){
				 if(obj instanceof Object[]){
					 Object[]temp=(Object[])obj;
					 HashMap<String,Object> map= new HashMap<String,Object>();
					 map.put("id", temp[0]);
					 map.put("name", temp[1]);
					 map.put("deviceName", temp[2]);
					 map.put("jiaJu", temp[3]);
					 map.put("descript", temp[4]);
					 map.put("lastUpdateTimeStr", SysUtils.formatDate(new Date(Long.parseLong(temp[5]+"")), "yyyy-MM-dd HH:mm:ss"));
					 result.add(map);
				 }
			 }
			pagination.setDatas(result);
			return pagination;
	}

	@Override
	public GongXuModel finishDeleteGongXuModel(Integer uid) {
		GongXuModel user=gongXuDao.get(GongXuModel.class, uid);
		if(user!=null){
			gongXuDao.delete(user);
		}
		return user;
	}

	@Override
	public GongXuModel finishUpdateGongXuModel(GongXuModel user) {
		GongXuModel lingJian=gongXuDao.get(GongXuModel.class, user.getId());
		if(lingJian!=null){
			lingJian.setLastUpdateTime(System.currentTimeMillis());
			gongXuDao.update(lingJian);
		}
		return lingJian;
	}

	@Override
	public LingJianModel finishLingJianByBianHao(String bianHao) {
		String hql="from LingJianModel where bianHao=?";
		LingJianModel  lingJian=lingJianDao.get(hql, bianHao);
		return lingJian;
	}

	@Override
	public List<GongXuModel> getGongXuModels() {
		return gongXuDao.find("from GongXuModel ");
	}

	@Override
	public LingJianGongXuModel finishAddLingJianGongXu(LingJianGongXuModel lingJianGongXuModel) {
		lingJianGongXuModel.setLastUpdateTime(System.currentTimeMillis());
		gongXuLjDao.save(lingJianGongXuModel);
		 return lingJianGongXuModel;
	}

	@Override
	public LingJianGongXuModel findLingJianGongXu(Integer uid) {
		
		return gongXuLjDao.get(LingJianGongXuModel.class, uid);
	}

	@Override
	public LinJianFactory finishAddCaigouInfo(LinJianFactory user) {
		user.setLastUpdateTime(System.currentTimeMillis());
		factoryLjDao.save(user);
		 return user;
	}

	@Override
	public DesignInfo finishAddDesignInfo(DesignInfo user) {
		user.setLastUpdateTime(System.currentTimeMillis());
		designInfoDao.save(user);
		 return user;
	}

	@Override
	public LinjianSellInfo finishAddSellInfo(LinjianSellInfo user) {
		user.setLastUpdateTime(System.currentTimeMillis());
		sellInfoDao.save(user);
		 return user;
	}

	@Override
	public void findLingJianInfo(Integer uid, ModelMap modelMap) {
		LingJianModel lingJianModel=lingJianDao.get(LingJianModel.class, uid);
		modelMap.put("lingJian", lingJianModel);
		//删除采购信息
		String hql=" from LinJianFactory where linJianId=?";
		List<LinJianFactory>factorys= factoryLjDao.find(hql, uid);
		modelMap.put("factorys", factorys);
		//删除加工信息
		 hql=" from LingJianGongXuModel where lingJianId=?";
		 List<LingJianGongXuModel>gongXus=gongXuLjDao.find(hql, uid);
		 modelMap.put("gongXus", gongXus);
		//删除设计信息
		 hql=" from DesignInfo where lingJianID=?";
		 List<DesignInfo>designInfos= designInfoDao.find(hql, uid);
		 modelMap.put("designInfos", designInfos);
		//删除销售信息
		 hql=" from LinjianSellInfo where linJianID=?";
		 List<LinjianSellInfo>sellInfos= sellInfoDao.find(hql, uid);
		 
		 modelMap.put("sellInfos", sellInfos);
	}

	@Override
	public LinJianKeyInfo finishAddLingJianKeyInfo(LinJianKeyInfo user) {
		user.setLastUpdateTime(System.currentTimeMillis());
		keyInfoDao.save(user);
		 return user;
	}

	@Override
	public Pagination<HashMap<String, Object>> selectAllKeyInfos(Integer page, int rows) {
		String sql="select count(*) from user_info,role where user_info.roleID=role.id ";
		long total=keyInfoDao.countSql(sql);
		Pagination<HashMap<String,Object>> pagination=new Pagination<HashMap<String,Object>>(page,(int) total, rows);
		List<HashMap<String,Object>>result=new ArrayList<HashMap<String,Object>>();
		sql="select user_info.id, user_info.userName,user_info.lastUpdateTime,user_info.userTel,user_info.roleID,role.roleName,role.description ,user_info.userCode from user_info,role where user_info.roleID=role.id ";
		 List<Object>  datas = keyInfoDao.findSql(sql, page, pagination.getPageSize());
		 for(Object obj:datas){
			 if(obj instanceof Object[]){
				 Object[]temp=(Object[])obj;
				 HashMap<String,Object> map= new HashMap<String,Object>();
				 map.put("id", temp[0]);
				
				 result.add(map);
			 }
		 }
		pagination.setDatas(result);
		return pagination;
	}
}
